package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/7 0007.
 * 销售进度 Adapter
 */

public class SellProgressAdapter extends YfListAdapter<Map<String, String>> {
    private Context context;

    public SellProgressAdapter(ArrayList<Map<String, String>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sell_progress_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setVisibility(View.GONE);
        ImageView img = (ImageView) view.findViewById(R.id.id_placeholder);
        img.setVisibility(View.GONE);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        if (i == 0) {
            ((ViewHolder) viewHolder).icon.setImageResource(R.mipmap.ic_details_check);
            ((ViewHolder) viewHolder).time.setTextColor(context.getResources().getColor(R.color.yellow));
            ((ViewHolder) viewHolder).state.setTextColor(context.getResources().getColor(R.color.yellow));
            ((ViewHolder) viewHolder).remark.setTextColor(Color.parseColor("#999999"));
        } else {
            ((ViewHolder) viewHolder).icon.setImageResource(R.mipmap.ic_follow_time);
            ((ViewHolder) viewHolder).time.setTextColor(Color.parseColor("#555555"));
            ((ViewHolder) viewHolder).state.setTextColor(Color.parseColor("#555555"));
            ((ViewHolder) viewHolder).remark.setTextColor(Color.parseColor("#999999"));
        }
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ((ViewHolder) viewHolder).time.setText(myFmt.format(new Date(Long.valueOf(mData.get(i).get("time")))));
        ((ViewHolder) viewHolder).state.setText(mData.get(i).get("state"));

//        String[] resourceState = new String[]{"0", "2", "3", "5", "7","9","10","11"};
//        Arrays.asList(resourceState).contains(mData.get(i).get("resourceState"))&&
        if (mData.get(i).containsKey("remark")) {
            ((ViewHolder) viewHolder).remark.setVisibility(View.VISIBLE);
            JSONObject remarkJson = JSON.parseObject(mData.get(i).get("remark"));
            String remarkStr = "";
            if (null != remarkJson ){
                Iterator<String> it = remarkJson.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    remarkStr = remarkStr.concat("\n");
                    if( key.equals("已结佣") ){
                        JSONArray jsonArray = JSON.parseArray(remarkJson.getString(key));
                        for( int j = 0 ; j < jsonArray.size();j++ ){
                            if(j > 0 && j<jsonArray.size()){
                                remarkStr = remarkStr.concat("\n");
                            }
                            remarkStr = remarkStr.concat(key).concat("：").concat( jsonArray.get(j).toString().replace(",","元      ") );
                        }
                    }else {
                        remarkStr = remarkStr.concat(key).concat("：").concat(remarkJson.getString(key));
                    }
                }
            }
            ((ViewHolder) viewHolder).remark.setText(remarkStr.replaceFirst("\\n",""));
        } else {
            ((ViewHolder) viewHolder).remark.setVisibility(View.GONE);
        }

        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }
    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {

        TextView time;
        TextView state;
        TextView remark;
        ImageView icon;

        public ViewHolder(View itemView) {
            super(itemView);
            time = (TextView) itemView.findViewById(R.id.id_time);
            state = (TextView) itemView.findViewById(R.id.id_state);
            remark = (TextView) itemView.findViewById(R.id.id_remark);
            icon = (ImageView) itemView.findViewById(R.id.id_timeline_cion);
        }
    }


}

